package com.appkudos.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appkudos.controller.UserController;
import com.appkudos.model.User;
import com.appkudos.view.R;
import android.app.Activity;
import android.os.Bundle;

import android.content.Intent;
import android.database.Cursor;
import android.provider.Browser;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class UserFriendActivity extends Activity implements ViewBinder{
    ListView lv;
    Button addFriendButton;
	private Activity PARENTACTIVITY; 
	User user;
	User friend;
	UserController uc;
	ArrayList<User> friends;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_friend_list);
        PARENTACTIVITY = this;
        
	    // go to user profile page
	    Intent launchIntent = getIntent();
	    Bundle extras = launchIntent.getExtras();
	    uc = new UserController();
	    user = uc.getCurrentUser(extras);
	    friend = uc.getFriend(extras);
       
  	    // get listview from layout
  	    lv=(ListView)findViewById(R.id.my_friend_list);
  	    
  	    // get friends for this user
  	    if(friend == null){
  	    	friends = (ArrayList<User>) uc.getFriendsOfUser(user.getUsername());
  	    }else{
  	    	friends = (ArrayList<User>) uc.getFriendsOfUser(friend.getUsername());
  	    }
  	    
  	    // display friends
  	    displayMatchedFriends(friends);
  	  
		lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		      User friend = friends.get(position);
		      // When a user is clicked, go to the user's profile
		      Toast.makeText(getApplicationContext(), friend.getUsername()+" is clicked",Toast.LENGTH_SHORT).show();
		     
		      Intent intent = new Intent(PARENTACTIVITY, AppKudos.class);
  		      intent = uc.passCurrentUser(intent, user);
  		      intent = uc.passFriend(intent, friend);
  		      startActivity(intent);
		    }
		});
		
        // enable filter on text
  	    lv.setTextFilterEnabled(true);
  	    
  	    // open a new page when add friend is clicked
  	     addFriendButton = (Button) this.findViewById(R.id.b_add_friend);
         addFriendButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {     
              //   Intent intent = new Intent(PARENTACTIVITY, addFriend.class);
         	//	startActivity(intent);
            	   Toast.makeText(getApplicationContext(), "add friend button is clicked",
            			      Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(PARENTACTIVITY, AddFriendActivity.class);
                   intent = uc.passCurrentUser(intent, user);
           		   startActivity(intent);
             }
           });  	    // open a new page when add friend is clicked
  	   
         
         
   }
    
 // helper method to display matched users
	private void displayMatchedFriends(List list){
	  	Map<String, User> itemData;
	  	User matched;
	  	List<Map<String, User>> listData = new ArrayList<Map<String, User>>();
	  	
	  	// put the friends into a hashmap (used for input by simple adapter)
	  	for(int j=0; j<list.size();j++){
	  		itemData = new HashMap<String, User>(1);
	  		matched = (User) list.get(j);
	  		itemData.put("single_list_item", matched);
	  		listData.add(itemData);
	  	}    
	
	  	// bind simple adapter to the list view
	      SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.single_text_item,
	              new String[] {
	                  "single_list_item"
	              }, new int[] {
	                  R.id.single_list_item
	              });
	      
	      // bind viewBinder to adapter and set adapter to listview
	      simpleAdapter.setViewBinder(this);
	      lv.setAdapter(simpleAdapter);
	  }

	 @Override
	  public boolean setViewValue(View view, Object data, String stringRepresetation) {  	
		  	  // get data for the row
		  	  User matched = (User) data;
		      TextView tv = (TextView) view;
		      
		      // bind the matched user to textView
		      tv.setText(matched.getUsername());

//		      tv.setOnClickListener(new View.OnClickListener() {
//		          public void onClick(View v)
//		          {
//		        	   CheckedTextView ckbox = (CheckedTextView) v;
//		        	   String friend = ckbox.getText().toString();
//		 	           
//		 	           if(!ckbox.isChecked()){
//		 	        	   ckbox.toggle();
//		 	        	  uc.addFriend(user.getUsername(),friend);
////		 	        	  Toast.makeText(getApplicationContext(), " friend is "+friend,Toast.LENGTH_SHORT).show();
//		 	        	  Toast.makeText(getApplicationContext(), ""+ friend+" has been added to your friends",Toast.LENGTH_SHORT).show();
//		 	           }
//		 	           	    
//		          }
//		      });

		      return true;
	  }
}
